import Router from 'koa-router';
import { connectProducer, processOrQueueMessage, producer } from '../../config/kafkaConfig';
import Result from '../../vo/result';
import { createOrderService } from '../../service/OrderService';
import logger from '../../Logger/logger';
import { routes } from '../../routes';

const router = new Router({
  prefix: '/api',
});

// 全局连接 Kafka 生产者
connectProducer().then(() => {
  console.log('Kafka producer connected');
});

router.get(routes.ORDER_CREATE, async ctx => {
  //只保留18位
  const id = (new Date().getTime().toString() + Math.random().toString(36)).substring(2, 18);

  //await processOrQueueMessage('orderTopic', id, 500, () => createOrderService(id));
  await producer.send({
    topic: 'orderTopic',
    messages: [
      { value: id },
    ],
  });
  ctx.body = Result.success('订单正在处理中');
});

export default router.routes();